package de.breitbeil.jbooking.ui.form;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import org.springframework.binding.form.FormModel;
import org.springframework.richclient.settings.Settings;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import de.breitbeil.jbooking.ui.preferences.SettingsFormObject;

public class FtpCalendarFormObject implements SettingsFormObject {

    private String host;
    private int port;
    private String username;
    private String password;
    private Date firstDisplayedDay;
    private String suffix;
    private String remoteFile;

    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void setFirstDisplayedDay(Date year) {
        this.firstDisplayedDay = year;
    }
    public Date getFirstDisplayedDay() {
        return firstDisplayedDay;
    }
    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
    public String getSuffix() {
        return suffix;
    }
    public void setRemoteFile(String remoteFile) {
        this.remoteFile = remoteFile;
    }
    public String getRemoteFile() {
        return remoteFile;
    }

    public void init(Settings settings) {
        host = settings.getString("ftp.host");
        port = settings.getInt("ftp.port");
        username = settings.getString("ftp.username");
        password = decode(settings.getString("ftp.password"));
        suffix = settings.getString("ftp.suffix");
        remoteFile = settings.getString("ftp.remoteFile");
        firstDisplayedDay = Calendar.getInstance().getTime();
    }

    public void save(Settings settings) {
        settings.setString("ftp.host", host);
        settings.setInt("ftp.port", port);
        settings.setString("ftp.username", username);
        settings.setString("ftp.password", encode(password));
        settings.setString("ftp.suffix", suffix);
        settings.setString("ftp.remoteFile", remoteFile);
    }

    public void fillDefaults(FormModel formModel) {
        String year = Calendar.getInstance().get(Calendar.YEAR)+"";

        formModel.getValueModel("host").setValue("");
        formModel.getValueModel("port").setValue(21);
        formModel.getValueModel("username").setValue("");
        formModel.getValueModel("password").setValue("");
        formModel.getValueModel("suffix").setValue("jpg");
        formModel.getValueModel("year").setValue(year);
        formModel.getValueModel("remoteFile").setValue("JBooking");
    }

    public static String encode(String password) {
        if(password == null)
            return "";

        return new BASE64Encoder().encode(password.getBytes());
    }

    public static String decode(String password) {
        if(password==null || password.equals(""))
            return "";

        try {
            return new String((new BASE64Decoder()).decodeBuffer(password));
        }
        catch(IOException e) {
            return "";
        }
    }
}
